package com.java.admin.controller;

import com.java.admin.service.ProductService;
import com.java.admin.utils.AliyunOSSUtils;
import com.java.param.AdminProductParam;
import com.java.param.ProductIdRemoveParam;
import com.java.param.ProductSearchParam;
import com.java.pojo.Product;
import com.java.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private AliyunOSSUtils aliyunOSSUtils;

    @GetMapping("/list")
    public R list(ProductSearchParam productSearchParam){
        return productService.list(productSearchParam);
    }
    @PostMapping("/upload")
    public R adminUpload(@RequestParam("img") MultipartFile img) throws Exception {
        String fileName = img.getOriginalFilename();
        fileName = UUID.randomUUID().toString().replaceAll("-","")+fileName;

        String contentType = img.getContentType();
        byte[] content = img.getBytes();
        int hours = 1000;
        String url = aliyunOSSUtils.uploadImage(fileName, content, contentType, hours);
        System.out.println("url = "+url);
        return R.ok("图片上传成功！",url);

    }
    @PostMapping("/save")
    public R save(AdminProductParam adminProductParam){
        return productService.save(adminProductParam);
    }

    @PostMapping("/update")
    public R update(Product product){
        return productService.update(product);
    }

    @PostMapping("/remove")
    public R remove(ProductIdRemoveParam productIdRemoveParam){
        return productService.remove(productIdRemoveParam);
    }

}
